﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data.SqlClient;

public partial class products : System.Web.UI.Page
{
    private string connectionString = WebConfigurationManager.ConnectionStrings["bugger"].ConnectionString;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.IsPostBack == false)
        {
            SqlConnection productConnection = new SqlConnection(connectionString);
            string selectSql = "SELECT productname, productid FROM products";

            productConnection.Open();

            SqlCommand selectCommand = new SqlCommand(selectSql, productConnection);

            SqlDataReader productReader;
            productReader = selectCommand.ExecuteReader();

            while (productReader.Read())
            {
                ListItem newItem = new ListItem();
                newItem.Text = productReader["productname"].ToString();
                newItem.Value = productReader["productid"].ToString();
                lstProductName.Items.Add(newItem);
            }

            lstProductName.ClearSelection();

            productReader.Close();
            productConnection.Close();
        }
    }

    protected void lstProductName_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection versionConnection = new SqlConnection(connectionString);
        string selectSql = "SELECT productversion FROM version WHERE productid = @productid ORDER BY productid ASC";
        
        versionConnection.Open();
        
        SqlCommand selectCommand = new SqlCommand(selectSql, versionConnection);
        int pid = Convert.ToInt32(lstProductName.SelectedItem.Value);
        selectCommand.Parameters.AddWithValue("@productid", pid);
        
        SqlDataReader versionReader;
        versionReader = selectCommand.ExecuteReader();

        lstProductVersion.Items.Clear();
        
        while (versionReader.Read())
        {
            ListItem newItem = new ListItem();
            newItem.Text = versionReader["productversion"].ToString();
            newItem.Value = versionReader["productversion"].ToString();

            lstProductVersion.Items.Add(newItem);
        }

        versionReader.Close();
        versionConnection.Close();
    }
    protected void btnShowDetails_Click(object sender, EventArgs e)
    {
        string url = "productdetail.aspx?";
        url += "ProductID=" + lstProductName.SelectedValue + "&";
        url += "ProductVersion=" + lstProductVersion.SelectedItem.Text;
        Response.Redirect(url);
    }
    protected void btSubmitBug_Click(object sender, EventArgs e)
    {
        string url = "submitbug.aspx?";
        url += "ProductID=" + lstProductName.SelectedValue + "&";
        url += "ProductVersion=" + lstProductVersion.SelectedItem.Text;
        Response.Redirect(url);
    }
}